//
//  ShardPageController.m
//  Pianke
//
//  Created by BetaTheta on 16/1/16.
//  Copyright © 2016年 BetaTheta. All rights reserved.
//

#import "ShardPageController.h"
#import "ShardCell.h"
#import "ShardModel.h"
#import <AFNetworking/AFNetworking.h>
#import <MJRefresh/MJRefresh.h>
#import "UIImageView+WebCache.h"
#import "PWNetWorking.h"

@interface ShardPageController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *shardTableView;
@property (nonatomic, strong) NSMutableArray *shardDataArr;

@property(nonatomic, assign) NSInteger i;/**<保存start的值*/
@property(nonatomic, assign) NSInteger time;/**<保存 addtime 的值*/
@end

@implementation ShardPageController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // tableView
    [self creatTableView];
    // data
    [self dataHandler];
    
//    // 下拉刷新
//    [self start];
//    // 赋初值
//    self.i = 0;
//    NSDate *date = [NSDate date];
//    _time = (long)[date timeIntervalSince1970];
//    self.shardDataArr = [NSMutableArray array];
//    
//    [self up];
}
#pragma mark - 下拉刷新
- (void)start
{
    // 弱引用
    // __block UIViewController *vc = self;
    // 设置回调(一旦进入刷新状态就会调用这个 refreshingBlock)
    // 设置回调(一旦进入刷新状态,就调用 target 的 action, 也就是调用 self 的 loadNewdata 方法)
    self.shardTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //        [self.dataArr removeAllObjects];
        self.i = 0;
        [self dataHandler];
    }];
    
}

- (void)up
{
    self.shardTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // NSDate *date = [NSDate date];
        //  NSTimeInterval time = (long)[date timeIntervalSince1970];
        //  _time -= (time - _time);
        _i += 10;
        [self dataHandler];
        //        [self.tableView.mj_footer endRefreshing];
        
    }];
    
}

- (void)creatTableView
{
    self.shardTableView = [[UITableView alloc] initWithFrame:self.view.frame];
    _shardTableView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_shardTableView];
    _shardTableView.delegate = self;
    _shardTableView.dataSource = self;
    [_shardTableView release];
    
    [_shardTableView registerClass:[ShardCell class] forCellReuseIdentifier:@"shardcell"];
    
}
- (void)dataHandler
{

    NSString *shardStr = @"http://api2.pianke.me/timeline/list";
    NSDictionary *shardBodyDic = @{@"auth": @"C5VuFSS49v9DCsJzHMbslDJxQMqvZraIm325tCj1lcWP229VPSRVc7f4", @"client": @"1", @"deviceid": @"DB3A1424-3E92-41D9-8C86-10F84629D968", @"limit": @"10", @"start": @"0", @"version": @"3.0.6"};
    [PWNetWorking POSTWithURL:shardStr body:shardBodyDic completion:^(id result) {
        self.shardDataArr = [ShardModel modelHanderWithArray:result[@"data"][@"list"]];
//        NSArray *tempArr = [ShardModel modelHanderWithArray:_shardDataArr];
//        if (_i == 0) {
//            [_shardDataArr removeAllObjects];
//        }
//        for (ShardModel *m in tempArr) {
//            [self.shardDataArr addObject:m];
//        }
//        [self.shardTableView.mj_header endRefreshing];
//        [self.shardTableView.mj_footer endRefreshing];
        NSLog(@"%@", result);
        [_shardTableView reloadData];
        
    }];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _shardDataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShardCell *shardCell = [tableView dequeueReusableCellWithIdentifier:@"shardcell" forIndexPath:indexPath];
    ShardModel *sM = _shardDataArr[indexPath.row];
    
    [shardCell.iconImg sd_setImageWithURL:[NSURL URLWithString:sM.icon]];
    shardCell.nameLabel.text = sM.uname;
    shardCell.timeLabel.text = sM.addtime_f;
    shardCell.coverImg_wh = sM.coverimg_wh;
    [shardCell.coverImg sd_setImageWithURL:[NSURL URLWithString:sM.coverimg]];
    shardCell.contentLabel.text = sM.content;
    shardCell.commentCount.text = [sM.comment stringValue];
    shardCell.likeCount.text = [sM.like stringValue];
    
    shardCell.tagLabel.text = sM.tag ;
    shardCell.tagCount.text = [[sM.count stringValue] stringByAppendingString:@"条"];
    
    
    return shardCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShardModel *sM = _shardDataArr[indexPath.row];
    CGFloat textH = [ShardCell heightWithText:sM.content];
    if (sM.coverimg.length) {
        CGFloat imgH = [ShardCell heightWithImage:sM.coverimg_wh];
        return 150 + imgH + textH;
    }else{
        return 150 + textH;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
    
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
